﻿
using System;
using System.Collections.Generic;
using AgileBoard.Domain;

namespace AgileBoard.Dtos.Mappers
{
    public class UserDTOMapper : BaseDTOMapper
    {
        public static UserDTO MapToDTO(User model, bool includeFeelings)
        {
            IList<FeelingsDTO> feelingsDTO = new List<FeelingsDTO>();

            var dto = new UserDTO();
            dto.UserID = model.UserID;
            dto.ID = model.UserID.ToString();
            dto.Name = model.UserName;
            dto.Mobile = model.Mobile;
            dto.Telephone = model.Telephone;
            dto.ValidFrom = model.ValidFrom;
            dto.ValidTo = model.ValidTo;
            dto.Email = model.Email;
            dto.CreateDate = model.CreateTime;
            dto.Role = model.Role == null ? "" : model.Role.Name;

            if (includeFeelings)
            {
                //load features
                foreach (Feelings feeling in model.Feelings)
                {
                    feelingsDTO.Add(FeelingsDTOMapper.MapToDTO(feeling));
                }
                dto.feelings = feelingsDTO;
            }

            return dto;
        }

        public static User MapFromDTO(UserDTO dto)
        {
            var user = new User();
            user.UserID = dto.UserID;
            user.Role = (dto.RoleID == null) ? null : ManagerFactory.RoleManager.GetById(dto.RoleID);
            user.UserName = dto.Name;
            user.Mobile = dto.Mobile;
            user.Telephone = dto.Telephone;
            user.ValidFrom = dto.ValidFrom;
            user.ValidTo = dto.ValidTo;
            user.Email = dto.Email;
            user.CreateTime = DateTime.Now;
            return user;
        }
    }
}